//
//  LTFHeadLineContentViewController.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/28.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFHeadLineContentViewController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import <WebKit/WebKit.h>
#import "LTFHeadLineWebModel.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface LTFHeadLineContentViewController ()
<
 UIWebViewDelegate
>
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) LTFHeadLineWebModel *model;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UIButton *button;
@end

@implementation LTFHeadLineContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 24, WIDTH, 40)];
    _headView.backgroundColor = [UIColor whiteColor];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [_headView addSubview:_button];
    _button.frame = CGRectMake(5, 0, 70, 40);
    [_button setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    _button.tintColor = [UIColor blackColor];
    [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self createView];
}
- (void)click:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.view addSubview:_headView];
}
- (void)viewWillDisappear:(BOOL)animated {
    [_headView removeFromSuperview];
}
- (void)createView {
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64)];
    [self.view addSubview:_webView];
    _webView.delegate = self;
}

- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string copy];
        [self createData];
    }
}

- (void)createData {
    NSString *htmlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.string];
    NSString *string = [htmlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSDictionary *dic = [dictionary objectForKey:self.string];
        self.model = [[LTFHeadLineWebModel alloc] init];
        [self.model setValuesForKeysWithDictionary:dic];
        [self loadMyWebView];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}


- (void)loadMyWebView
{
    [self loadIMG];
    [self loadVIDEO];
    self.model.body = [NSString stringWithFormat:@"<h2>%@</h2>%@  %@\n%@\n", self.model.title, self.model.source, self.model.ptime, self.model.body];
    [self.webView loadHTMLString:self.model.body baseURL:nil];
}

- (void)loadIMG
{
    for (NSInteger i = 0; i < self.model.img.count; i++) {
        
        NSString *length = [[self.model.img objectAtIndex:i] objectForKey:@"pixel"];
        NSDictionary *dic = [self.model.img objectAtIndex:i];
        
        
        NSArray *arr = [length componentsSeparatedByString:@"*"];
        NSString *imgStr = [NSString stringWithFormat:@"<!--IMG#%ld-->", i];
        // 拼接
        CGFloat height = [arr[0] integerValue];
        CGFloat width = [arr[1] integerValue];
        width = self.view.frame.size.width - 20;
        height = width * 0.6;
        
        NSString *str = [NSString stringWithFormat:@"<img src='%@'  height='%f' width='%f' />\n",[dic objectForKey:@"src"], height, width];
        
        // 拼接内容
        self.model.body = [self.model.body stringByReplacingOccurrencesOfString:imgStr withString:str];
    }
}
- (void)loadVIDEO {
    for (NSInteger i = 0; i < self.model.video.count; i++) {
        NSDictionary *dic = self.model.video[i];
        NSString *videoStr = [NSString stringWithFormat:@"<!--VIDEO#%ld-->", i];
        NSString *url = dic[@"url_mp4"];
        CGFloat width = 375 - 20;
        CGFloat height = width * 0.6;
        NSString *str = [NSString stringWithFormat:@"<video src ='%@'  height='%f' width='%f' poster = '%@'/>\n",url, height, width, dic[@"cover"]];
        // 拼接内容
        self.model.body = [self.model.body stringByReplacingOccurrencesOfString:videoStr withString:str];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
